﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using iPhoneAppGenre.models;
using iPhoneAppGenre.models.ipaDataSetTableAdapters;

namespace iPhoneAppGenre.views
{
    public partial class FormMain : Form
    {
        ipaFile[] ipas;

        public FormMain()
        {
            InitializeComponent();
        }
        #region NEWCODE
        private void toolStripButtonSelectFolder_Click(object sender, EventArgs e)
        {
            if (selectFolder() == DialogResult.OK)
            {
                reloadView();
            }
        }

        private DialogResult selectFolder()
        {
            DialogResult result = folderBrowserDialogIPAFolder.ShowDialog();
            if (result == DialogResult.OK)
            {
                ipas = new ipaAppFolder(folderBrowserDialogIPAFolder.SelectedPath).ipas;
            }
            return result;
        }

        private void reloadView()
        {
            this.ipaDataSet.ipafile.Clear();
            foreach (ipaFile ipa in ipas)
            {
                if (!ipa.ipaHasMetadata)
                {
                    ipaDataSet.ipafileRow ifr = this.ipaDataSet.ipafile.NewipafileRow();
                    ifr.id = ipa.dbid;
                    ifr.genrecode = "0000";
                    ifr.location = ipa.ipaLocationName;
                    ifr.name = ipa.ipaName;
                    ifr.seller = ipa.ipaSeller;

                    this.ipaDataSet.ipafile.AddipafileRow(ifr);
                    this.ipaDataSet.ipafile.AcceptChanges();
                }
            }
        }
        #endregion

        private void FormMain_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'ipaDataSet.ipa' table. You can move, or remove it, as needed.
            this.ipafileTableAdapter.Fill(this.ipaDataSet.ipafile);
            // TODO: This line of code loads data into the 'ipaDataSet.genre' table. You can move, or remove it, as needed.
            this.genreTableAdapter.Fill(this.ipaDataSet.genre);
        }

        private void dataGridViewIPAFiles_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {

                DataGridViewRow dr = dataGridViewIPAFiles.Rows[e.RowIndex];
                ipaFile ipa = ipas[e.RowIndex];
                switch (e.ColumnIndex)
                {
                    case 1: //name
                        ipa.ipaName = dr.Cells[1].Value.ToString();
                        break;
                    case 2: //seller
                        ipa.ipaSeller = dr.Cells[2].Value.ToString();
                        break;
                    case 3: //genre
                        //int genreid = dr.Cells[3].
                        //ipa.ipaGenreName = dr.Cells[3].Value.ToString();
                        //ipa.ipaGenreCode = dr.Cells[3].Value.ToString();
                        break;
                    default:
                        break;
                }
                //MessageBox.Show(dr.Cells[e.ColumnIndex].Value.ToString());
            }
        }

    }
}
